package com.aqie.easy.binarySearchTree;

import com.aqie.easy.structure.TreeNode;

import java.util.ArrayList;

/**
 * 530 二叉搜索树 任意两节点差的绝对值的最小值
 * todo
 */
public class GetMinimumDifference {
    private int minDiff = 9999;
    ArrayList<Integer> vals = new ArrayList<>();
    public int getMinimumDifference(TreeNode root) {
        return saveValues(root);
    }

    private int saveValues(TreeNode root){
        if (root == null) return minDiff;
        if (root.left != null) getMinimumDifference(root.left);

        vals.add(root.val);
        if (vals.size() >= 2 && vals.get(vals.size() - 1) - vals.get(vals.size() - 2) < minDiff)
            minDiff = vals.get(vals.size() - 1) - vals.get(vals.size() - 2);
        if (root.right != null) getMinimumDifference(root.right);
        return minDiff;
    }
}
